package com.thinking.project.monitorserver.controller;

import com.thinking.project.monitorserver.pojo.Department;
import com.thinking.project.monitorserver.service.DepmentService;
import com.thinking.project.monitorserver.util.ResponseData;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Description 功能描述：
 * @Author tangsixiang@163.com
 * @DATE 2019/6/21 11:27
 * @Version 1.0
 **/

@Slf4j
@RestController
@RequestMapping(value = "/department")
public class DepartmentController {

    @Autowired
    DepmentService depmentService;


    @RequestMapping(value = "/addDepartment", method = {RequestMethod.POST, RequestMethod.GET})
    public ResponseData addDepartment(@RequestBody Department department) {
        log.debug(department + "----");
        int saveNum = depmentService.insert(department);
        if (saveNum == 1) {
            return ResponseData.ok().putDataValue("result", department);
        } else {
            return ResponseData.forbidden().putDataValue("result", department);
        }
    }

    @RequestMapping(value = "/updateDepartment", method = {RequestMethod.POST, RequestMethod.GET})
    public ResponseData updateDepartment(@RequestBody Department department) {
        int updateNum = depmentService.updateByPrimaryKey(department);
        if (updateNum == 1) {
            return new ResponseData(updateNum, "修改成功");
        } else {
            return new ResponseData(updateNum, "修改异常");
        }
    }


    @RequestMapping(value = "/queryDepartment", method = {RequestMethod.POST, RequestMethod.GET})
    public ResponseData queryDepartById(@RequestBody Department department) {
        return ResponseData.ok().putDataValue("depart", depmentService.selectByPrimaryKey(department.getDepId()));

    }


    /**
     * @return java.util.List<com.thinking.project.monitorserver.pojo.Department>
     * @Author tangsixiang
     * @Description 查询列表
     * @Date 13:24 2019/6/21
     * @Param [department]
     ****/
    @RequestMapping(value = "/queryDepartmentList", method = {RequestMethod.POST, RequestMethod.GET})
    public ResponseData queryDepartList(@RequestBody Map<String, Object> department) {
        return ResponseData.ok().putDataValue("departList", depmentService.selectForList(department));

    }


    @RequestMapping(value = "/deleteDepartment", method = {RequestMethod.POST, RequestMethod.GET})
    public ResponseData deleteDepartment(@RequestBody Department department) {
        int delNum = depmentService.deleteByPrimaryKey(department.getDepId());
        if (delNum == 1) {
            return new ResponseData(delNum, "删除成功");
        } else {
            return new ResponseData(delNum, "删除异常");
        }
    }


}
